package script.mainpage.items
{
	import laya.events.Event;
	import laya.utils.Handler;
	
	import model.order.OrderVo;
	
	import script.ViewManager;
	
	import ui.items.OrderItemUI;
	
	import utils.UtilTool;
	
	public class UserOrderCell extends OrderItemUI
	{
		private var ordervo:OrderVo;
		private var createDate:Date;
		public function UserOrderCell()
		{
			super();
			
		}
		private function updateGoodsList(cell:SimpleOrderGoodsCell):void
		{
			cell.setData(cell.dataSource);
		}
		
		public function setData(data:*):void
		{
			this.ordervo = data;
			
			this.orderTime.text = ordervo.createTime;
			
			this.totalMoney.text = "" + ordervo.orderAmount;
			
			while(this.goodslist.numChildren > 0)
				this.goodslist.removeChildAt(0);
			
			var goodscount:int = 0;
			if(ordervo.orderGoodsVoList != null)
			{
				for(var i=0;i < ordervo.orderGoodsVoList.length;i++)
				{
					goodscount += ordervo.orderGoodsVoList[i].goodsCount;
					var goodsCell:SimpleOrderGoodsCell = new SimpleOrderGoodsCell();
					goodsCell.goodsPic.skin = ordervo.orderGoodsVoList[i].goodsImg;
					goodsCell.numlbl.text = "x" + ordervo.orderGoodsVoList[i].goodsCount;
					this.goodslist.addChild(goodsCell);
					goodsCell.x = 100 * i;
				}
					
			}
			//goodslist.array = ordervo.orderGoodsVoList;
			
			
			this.goodsNum.text = "共" + goodscount + "件";
			
			//var ymd:Array = 
			createDate = new Date(ordervo.createTime);
			
			this.payBtn.on(Event.CLICK,this,showPayPanel);
			Laya.timer.clearAll(this);

			this.on(Event.CLICK,this,function(){
				
				ViewManager.instance.openView(ViewManager.VIEW_ORDER_DETAIL_PANEL,false,ordervo);
			});
			if(ordervo.orderState == 0)
			{
				this.lefttips.visible = true;

				Laya.timer.loop(1000,this,countDown);
				this.tipsPay.visible = true;
			}
			else
			{
				this.lefttips.visible = false;
				this.payBtn.visible = false;
				
				
				var pickdate:Date = new Date(createDate.getTime() + 24*3600*1000);
				this.leftPayTime.text = "已支付，待取货,预计" + UtilTool.formatFullDateTime(pickdate,false) + "上午9点半后可取货";

				this.tipsPay.visible = false;

			}
		}
		
		private function showPayPanel(e:Event):void
		{
			e.stopPropagation();
			ViewManager.instance.openView(ViewManager.VIEW_CONFIRM_PAY_PANEL,false,{money:ordervo.orderAmount,orderId:ordervo.orderId});

		}
		private function countDown():void
		{
			var leftime = (new Date()).getTime() - createDate.getTime();
			
			if(leftime/1000 > 3600)
			{
				this.leftPayTime.text = "支付已超时";
				Laya.timer.clearAll(this);
			}
			else
			{
				this.leftPayTime.text = "00:" + UtilTool.getCountDownString(3600 - Math.floor(leftime/1000));
			}
		}
	}
}